clear
clear matrix
clear mata
set matsize 4000
set maxvar 10000

*Set to local directory
*cd ""

run "scripts/programs/program_data.do"
run "scripts/programs//program_regs.do"
run "scripts/programs/program_regtable.do"
run "scripts/programs/program_coefmat.do"
run "scripts/programs/program_coefscatter.do"


/*********************************************************************************/
*Set program parameters
local radius = 20
local angle = 15
local length = 1
/**********************************************************************************/

/***************************** DIST LAG TABLE - Assessors *************************/

/************************************** HU Data ***********************************/

use "data/`radius'-by-`length'km_`angle'deg/data_centroid.dta", clear
merge m:1 firenum using ///
	"data/`radius'-by-`length'km_`angle'deg/sample_assessors.dta", ///
	nogen

label var roadpcttot "Area $< 0.5$ km from road"
label var tmissing "$\Delta T$ missing"
label var lnt "Ln($\Delta T$ + 1)"
label var habpolytot "TES habitat"
label var wsimptot "Watershed importance"

local controls lnt tmissing lnint wind_diff majorroad
local lags any_res count_res avgval_res val_res habpolytot wsimptot campground wilderness
local costlags tritot roadpcttot

/**********************************	 Run regressions ******************************/

local cond `"complexfire != 1 & year > 2011 & sample == 1"'

distlag y `controls'  if `cond', ///
	method(unrestricted) params(5) lagvars(`lags') costlags(`costlags') ///
	burnt(centroid) tmethod(log) fe(firenum) cluster(firenum) output(table)

/**************************************  Make table  ***********************************/

local vars = substr("`e(indepvars)'",1,strpos("`e(indepvars)'","2bn.distnum")-1)

local j = 0
foreach var in `vars' {

	di "`var'"
	local j = `j' + 1
	if substr("`var'",1,1) == "F" local varname = substr("`var'",3,.)
	else local varname "`var'"
	local lab: variable label `varname'
	local labels `"`labels' "`lab'" "'
	if `j' == 1 mat def blag = [_b[`var'], _se[`var']]
	else mat def blag = [blag \ _b[`var'], _se[`var']]

	}

mat rownames blag = `vars'
mat colnames blag =  "b" "se"
svmat blag, names(col)
keep b se

gen varname = ""
gen number = .
local j = 0
foreach var in `vars' {
	local j = `j' + 1
	replace number = `j' if _n == `j'
	replace varname = "`var'" if _n == `j'
	}

gen label = ""
local j = 0
di `"`labels'"'
foreach lab in `labels' {
	local j = `j' + 1
	replace label = "`lab'" if _n == `j'
	}

bysort label: egen minnum = min(number)
drop number

gen lead = substr(varname,2,1) if substr(varname,1,1) == "F"
destring lead, replace
gen name = substr(varname,3,.) if substr(varname,1,1) == "F"
drop if b == .
replace name = varname if lead == .
replace lead = 0 if lead == .
drop varname

gen pval = 1-normal(abs(b/se))
gen stars = cond(pval < .01,"**",cond(pval < .05,"*",cond(pval < .1,"+","")))
tostring b, replace format(%8.2gc) force
tostring se, replace format(%8.2gc) force
replace b = b + stars
replace se = "(" + se + ")"
drop pval stars name

reshape wide b se, i(label) j(lead)

foreach var of varlist b0-se5 {
	local leadnum = substr("`var'",-1,.)
	if substr("`var'",1,1) == "b" rename `var' F`leadnum'1
	else if substr("`var'",1,1) == "s" rename `var' F`leadnum'2
	}

reshape long F0 F1 F2 F3 F4 F5, i(label) j(number)
sort minnum number
replace label = "" if number == 2
drop minnum number

local n = _N + 3
set obs `n'
replace label = "Fire fixed effects" if _n == `n'-2
replace F0 = "Yes" if _n == `n'-2
replace label = "Number obs." if _n == `n'-1
replace F0 = "`e(N)'" if _n == `n'-1
replace label = "Number fires" if _n == `n'
replace F0 = "`e(N_clust)'" if _n == `n'

replace F5 = F5 + "\\"

local dir "Results/August_2021/Appendix_Assessor_Unrestricted/"
capture mkdir `dir'
export delimited using "`dir'table_contents.tex", delim("&") novarnames replace


/***************************** DIST LAG TABLE - Census *************************/

/************************************** HU Data ***********************************/

use "data/`radius'-by-`length'km_`angle'deg/data_centroid.dta", clear
merge m:1 firenum using ///
	"data/`radius'-by-`length'km_`angle'deg/sample_census.dta", ///
	nogen

local controls lnt tmissing lnint wind_diff majorroad
local lags anyhu hu inc_pcincome huxpcinc habpolytot wsimptot campground wilderness
local costlags tritot roadpcttot

label var roadpcttot "Area $< 0.5$ km from road"
label var tmissing "$\Delta T$ missing"
label var lnt "Ln($\Delta T$ + 1)"
label var habpolytot "TES habitat"
label var wsimptot "Watershed importance"

/**********************************	 Run regressions ******************************/

local cond `"complexfire != 1 & sample == 1"'

distlag y `controls'  if `cond', ///
	method(unrestricted) params(5) lagvars(`lags') costlags(`costlags') ///
	burnt(centroid) tmethod(log) fe(firenum) cluster(firenum) output(table)


/***********************************  Make table  *********************************/

local vars = substr("`e(indepvars)'",1,strpos("`e(indepvars)'","2bn.distnum")-1)

local j = 0
local labels
foreach var in `vars' {

	local j = `j' + 1
	if substr("`var'",1,1) == "F" local varname = substr("`var'",3,.)
	else local varname "`var'"
	local lab: variable label `varname'
	local labels `"`labels' "`lab'" "'
	if `j' == 1 mat def blag = [_b[`var'], _se[`var']]
	else mat def blag = [blag \ _b[`var'], _se[`var']]

	}

mat rownames blag = `vars'
mat colnames blag =  "b" "se"
svmat blag, names(col)
keep b se

gen varname = ""
gen number = .
local j = 0
foreach var in `vars' {
	local j = `j' + 1
	replace number = `j' if _n == `j'
	replace varname = "`var'" if _n == `j'
	}

gen label = ""
local j = 0
di `"`labels'"'
foreach lab in `labels' {
	local j = `j' + 1
	replace label = "`lab'" if _n == `j'
	}

bysort label: egen minnum = min(number)
drop number

gen lead = substr(varname,2,1) if substr(varname,1,1) == "F"
destring lead, replace
gen name = substr(varname,3,.) if substr(varname,1,1) == "F"
drop if b == .
replace name = varname if lead == .
replace lead = 0 if lead == .
drop varname

gen pval = 1-normal(abs(b/se))
gen stars = cond(pval < .01,"**",cond(pval < .05,"*",cond(pval < .1,"+","")))
tostring b, replace format(%8.2gc) force
tostring se, replace format(%8.2gc) force
replace b = b + stars
replace se = "(" + se + ")"
drop pval stars name

reshape wide b se, i(label) j(lead)

foreach var of varlist b0-se5 {
	local leadnum = substr("`var'",-1,.)
	if substr("`var'",1,1) == "b" rename `var' F`leadnum'1
	else if substr("`var'",1,1) == "s" rename `var' F`leadnum'2
	}

reshape long F0 F1 F2 F3 F4 F5, i(label) j(number)
sort minnum number
replace label = "" if number == 2
drop minnum number

local n = _N + 3
set obs `n'
replace label = "Fire fixed effects" if _n == `n'-2
replace F0 = "Yes" if _n == `n'-2
replace label = "Number obs." if _n == `n'-1
replace F0 = "`e(N)'" if _n == `n'-1
replace label = "Number fires" if _n == `n'
replace F0 = "`e(N_clust)'" if _n == `n'

replace F5 = F5 + "\\"

local dir "Results/Appendix_Census_Unrestricted/"
capture mkdir `dir'
export delimited using "`dir'table_contents.tex", delim("&") novarnames replace
